package org.example.transaction.service;


import org.example.transaction.mapper.LogInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

@Service
public class LogInfoService {

    @Autowired
    private LogInfoMapper logInfoMapper;

    /*
     * (加⼊事务)
     *  一个出错则进行全部回滚
     * */
    @Transactional(propagation = Propagation.REQUIRED)
    public Integer insertLogInfo1(String userName,String op){
        //发生异常情况，进行回滚
        int a = 10/0;
        return logInfoMapper.insertLogInfo(userName,op);
    }

    /*
    * (嵌套事务)
    * 出现一个异常，则回滚（可以部分回滚）
    * */
    @Transactional(propagation = Propagation.NESTED)
    public Integer insertLogInfo(String userName,String op){
        //发生异常情况，进行回滚
        try{
            int a = 10/0;
        }catch (Exception e){
            //执行部分回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return logInfoMapper.insertLogInfo(userName,op);
    }
}
